#include "AbstractUnit.h"
#include <typeinfo>
#include <math.h>
#include <iostream>

using namespace std;

bool AbstractUnit::isSameUnitType(const AbstractUnit& unit) const
{
   return typeid(*this) == typeid(unit);
}

float AbstractUnit::convertToBenchmark() const
{
  //convert to Base Unit
  return factorToBase_m;
}

AbstractUnit& AbstractUnit::operator=(const AbstractUnit& abstractUnitA)
{
   factorToBase_m = abstractUnitA.factorToBase_m;
   unitName_m = abstractUnitA.unitName_m;
   return *this;
}


bool AbstractUnit::operator==(const AbstractUnit& abstractUnitA) const
{

   return (unitName_m == abstractUnitA.unitName_m);
}

bool operator< (const AbstractUnit& abstractUnitA, const AbstractUnit& abstractUnitB)
{
     return abstractUnitA.convertToBenchmark() < abstractUnitB.convertToBenchmark();

}

ostream& operator<<(ostream& os, const AbstractUnit& unit)
{
  os << unit.unitName_m;
  return os;
}

void AbstractUnit::stripUnit( float &amountInThisUnit, float &remainingAmount) const
{
	amountInThisUnit = remainingAmount / factorToBase_m;
	remainingAmount = fmod(remainingAmount,factorToBase_m);	
}

void AbstractUnit::toString( ostream &os, float &amountInThisUnit, float &remainingAmount) const
{
	if ((int)(amountInThisUnit))
	{
		os << (int)(amountInThisUnit) << " " << unitName_m;
		if (remainingAmount > 0)
			os << " ";
	}
}

void AbstractUnit::formatThisUnit( ostream &os, float &remainingAmount) const
{
	float amountInThisUnit;

	stripUnit( amountInThisUnit, remainingAmount);
	toString( os, amountInThisUnit, remainingAmount);

}

